import QuickHull from '../algorithms/quickHull'

var max = 10000000
var min = -10000000
var startFrom = 0
var lastLength = 0;
function multiHull(data){
    var highPoints = [];
    var lowPoints = [];
    for(var i=0;i<data.length;i++){
        var dat = data[i];
        highPoints.push([dat.timestamp, dat.high])
        lowPoints.push([dat.timestamp, dat.low])
    }

    var highTopPoints = [];
    var lowTopPoints = [];
    var highBottomPoints = [];
    var lowBottomPoints = [];



    highTopPoints.push([data[0].timestamp, max])
    for(var i=0;i<highPoints.length;i++){
        highTopPoints.push([highPoints[i][0], highPoints[i][1]])
    }
    highTopPoints.push([data[highPoints.length - 1].timestamp, max])

    lowTopPoints.push([data[0].timestamp, max])
    for(var i=0;i<lowPoints.length;i++){
        lowTopPoints.push([lowPoints[i][0], lowPoints[i][1]])
    }
    lowTopPoints.push([data[lowPoints.length - 1].timestamp, max])

    highBottomPoints.push([data[0].timestamp, min])
    for(var i=0;i<highPoints.length;i++){
        highBottomPoints.push([highPoints[i][0], highPoints[i][1]])
    }
    highBottomPoints.push([data[highPoints.length - 1].timestamp, min])

    lowBottomPoints.push([data[0].timestamp, min])
    for(var i=0;i<lowPoints.length;i++){
        lowBottomPoints.push([lowPoints[i][0], lowPoints[i][1]])
    }
    lowBottomPoints.push([data[lowPoints.length - 1].timestamp, min])

    console.log("highTopPoints", highTopPoints)
    console.log("lowTopPoints", lowTopPoints)
    console.log("highBottomPoints", highBottomPoints)
    console.log("lowBottomPoints", lowBottomPoints)

    var highTopHull = QuickHull.quickHull(highTopPoints).map(c=>[c.x, c.y])
    var lowTopHull = QuickHull.quickHull(lowTopPoints).map(c=>[c.x, c.y])
    var highBottomHull = QuickHull.quickHull(highBottomPoints).map(c=>[c.x, c.y])
    var lowBottomHull = QuickHull.quickHull(lowBottomPoints).map(c=>[c.x, c.y])

    //sort by c.x
    var highTopHull = highTopHull.sort((a, b) => a[0] - b[0])
    var lowTopHull = lowTopHull.sort((a, b) => a[0] - b[0])
    var highBottomHull = highBottomHull.sort((a, b) => a[0] - b[0])
    var lowBottomHull = lowBottomHull.sort((a, b) => a[0] - b[0])

    return {
        highTopHull,
        lowTopHull,
        highBottomHull,
        lowBottomHull
    }
}
let lines = [];
const colors = ["#FF0000", "#00FF00", "#0000FF", "#FFFF00"]

function drawLine(chart, points, color){
    for(var i=0;i<points.length - 1;i++){
        var hpoint = points[i];
        var next = i + 1;
        if (next >= points.length) {
            next = 0;
            continue;
        }
        var npoint = points[next];
        console.log("draw line", hpoint, npoint)
        lines.push(chart.createOverlay({
            name: 'segment',
            // extendData: 'segment',
            points: [
                { timestamp: hpoint[0], value: hpoint[1] },
                { timestamp: npoint[0], value: npoint[1] },
            ],
            mode: 'weak_magnet',
            styles: {
                line: {
                    style: 'solid',
                    dashedValue: [2, 2],
                    color: color,
                    size: 5,
                }
            },
        }));

    }
}
function update(chart, data){
    lastLength = data.length
    data = data.slice(startFrom, data.length)
    console.log(data.length)
    if(data.length < 2){
        return
    }
    console.log("stock data", data)
    var hulls = multiHull(data)
    var highTopHull = hulls.highTopHull.filter(point => point[1] !== max && point[1] !== min)
    var lowTopHull = hulls.lowTopHull.filter(point => point[1] !== max && point[1] !== min)
    var highBottomHull = hulls.highBottomHull.filter(point => point[1] !== max && point[1] !== min)
    var lowBottomHull = hulls.lowBottomHull.filter(point => point[1] !== max && point[1] !== min)

    console.log("highTopHull", highTopHull)
    console.log("lowTopHull", lowTopHull)
    console.log("highBottomHull", highBottomHull)
    console.log("lowBottomHull", lowBottomHull)

    lines.forEach(line => {
        chart.removeOverlay(line)
    })
    drawLine(chart, highTopHull, colors[0])
    drawLine(chart, lowTopHull, colors[1])
    drawLine(chart, highBottomHull, colors[2])
    drawLine(chart, lowBottomHull, colors[3])
}

const comment = props => {
    return(<div>
        <div style={{display: 'flex', flexDirection: 'row', alignItems: 'center'}}>
            <div style={{width: 100, height: 5, backgroundColor: colors[0]}}></div><div style={{marginLeft: 15}}>顶凹</div>
        </div>
        <div style={{display: 'flex', flexDirection: 'row', alignItems: 'center'}}>
            <div style={{width: 100, height: 5, backgroundColor: colors[1]}}></div><div style={{marginLeft: 15}}>底凸</div>
        </div>
        <div style={{display: 'flex', flexDirection: 'row', alignItems: 'center'}}>
            <div style={{width: 100, height: 5, backgroundColor: colors[2]}}></div><div style={{marginLeft: 15}}>顶凸</div>
        </div>
        <div style={{display: 'flex', flexDirection: 'row', alignItems: 'center'}}>
            <div style={{width: 100, height: 5, backgroundColor: colors[3]}}></div><div style={{marginLeft: 15}}>底凹</div>
        </div>
    </div>)
}

const Tools = (props) => {
    return <div>未提供工具</div>;
}

export default {update, name: "多凸策略", comment, Tools};
